package com.gph.saviorframework.security.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gph.saviorframework.Constants;
import com.gph.saviorframework.FieldType;
import com.gph.saviorframework.common.model.Function;
import com.gph.saviorframework.dao.security.FunctionDao;
import com.gph.saviorframework.dao.security.ModuleDao;
import com.gph.saviorframework.security.service.FunctionService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

public class FunctionServiceImpl implements FunctionService {

    @Autowired
    private FunctionDao functionDao;

    @Autowired
    private ModuleDao moduleDao;

    private Map<String, String> fields = new HashMap<String, String>();

    public FunctionServiceImpl(){
        fields.put("FUNCTION_ID", FieldType.STRING);
        fields.put("FUNCTION_NAME",FieldType.STRING);
    }

    /**
     * 根据模块编号获取function列表
     *
     * @param moduleId
     * @return
     */
    public List<Function> getFunsByModuleId(String moduleId) {
        return functionDao.getFunsByModuleId(moduleId);
    }

    /**
     * 根据用户编号获取列表名称
     *
     * @param username
     * @return
     */
    public List<String> findFunctionsByUserId(String username) {
        return functionDao.findFunctionsByUserId(username);
    }

    /**
     * 根据模块编号和角色编号获取function Map
     *
     * @param moduleId
     * @param roleId
     * @return
     */
    public List<Map<String, Object>> findModuleAndFunctionByRole(String moduleId, String roleId) {
        if (moduleId.equalsIgnoreCase(Constants.TREE_ROOT_NODE)){
            return moduleDao.findModules();
        }
        else{
            return functionDao.findFunctionByRole(moduleId,roleId);
        }
    }

    /**
     * 根据模块编号和角色编号获取function Map，模块前面添加checkbox全选
     *
     * @param moduleId
     * @param roleId
     * @return
     */
    public List<Map<String, Object>> findModuleWithCheckboxAndFunctionByRole(String moduleId, String roleId) {
        if (moduleId.equalsIgnoreCase(Constants.TREE_ROOT_NODE)){
            return moduleDao.findModulesByRoleId(roleId);
        }
        else{
            return functionDao.findFunctionByRole(moduleId,roleId);
        }
    }

    /**
     * 保存
     *
     * @param function
     */
    public void save(Function function) {
        functionDao.save(function);
    }

    /**
     * 根据编号获取
     *
     * @param id
     * @return
     */
    public Function get(String id) {
        return functionDao.get(id);
    }

    /**
     * 更新
     *
     * @param function
     * @return
     */
    public void update(Function function) {
        functionDao.update(function);
    }

    /**
     * 批量更新
     *
     * @param functions
     */
    @Override
    public void update(Set<Function> functions) {
        for(Function function : functions){
            update(function);
        }
    }

    /**
     * 根据条件返回数据
     *
     * @param sort
     * @param dir
     * @param params
     * @return
     */
    public List<Map<String, Object>> find(String sort, String dir, Map<String, Object> params) {
        return functionDao.find(sort,dir,genWhereClause(params));
    }

    /**
     * 带分页参数的，给bootstrap使用的
     *
     * @param start  开始页码，从1开始
     * @param limit  每页查询的次数
     * @param sort
     * @param dir
     * @param params
     * @return
     */
    @Override
    public Map<String, Object> find(int start, int limit, String sort, String dir, Map<String, Object> params,Object... extras) {
        Map<String,Object> map = new HashMap<String, Object>();

        PageHelper.startPage(start, limit);
        List<Map<String,Object>> list = find(sort, dir, params);
        PageInfo pageInfo = new PageInfo(list);

        map.put(Constants.DEFAULT_RECORD_MODEL_KEY, list);
        map.put(Constants.DEFAULT_COUNT_MODEL_KEY, pageInfo.getPages());
        map.put(Constants.DEFAULT_RECORDS_MODEL_KEY,pageInfo.getTotal());
        map.put(Constants.DEFAULT_SUCCESS_KEY, Boolean.TRUE);

        return map;
    }

    /**
     * 统计数量
     *
     * @param params
     * @return
     */
    public long count(Map<String, Object> params) {
        return functionDao.count(genWhereClause(params));
    }

    /**
     * 根据主键删除
     *
     * @param id
     */
    public void delete(String id) {
        functionDao.deleteById(id);
    }

    /**
     * 批量删除
     *
     * @param ids
     */
    public void deleteByIds(String[] ids) {
        for(String id : ids){
            delete(id);
        }
    }

    /**
     * 查询字段集合
     *
     * @return
     */
    public Map<String, String> getFields() {
        return fields;
    }

    /**
     * 根据id查询map
     *
     * @param id
     * @return
     */
    public Map<String, Object> findById(String id) {
        return functionDao.findById(id);
    }

    public String genWhereClause(Map<String, Object> params,Object... extra) {
        String whereClause = "1=1";
        if (params!=null && params.keySet().size() > 0) {
            for (String key : params.keySet()) {
                if (fields.get(key).equals(FieldType.STRING)) {
                    whereClause += " and F." + key + " LIKE '%"+params.get(key)+"%'";
                } else {
                    whereClause += " and F." + key + " ='"+params.get(key)+"'";
                }
            }
        }
        return  whereClause;
    }
}
